package map;

import java.util.*;

public class MapDemo2 {
    public static void main(String[] args) {
        //map的遍历方式：遍历所有key，遍历所有键值对，遍历所有value
        Map<String,Integer> map=new HashMap<>();
        map.put("语文",99);
        map.put("数学",98);
        map.put("英语",97);
        map.put("物理",96);
        map.put("化学",99);
        System.out.println(map);

        //遍历所有key
        Set<String> keySet= map.keySet();//map的key是不可重复，所有可以用set的集合接收
        for (String key:keySet){
            System.out.println("key:"+key);
        }
        Iterator it= keySet.iterator();
        while (it.hasNext()){
            System.out.println("key:"+it.next());
        }

        //遍历所有键值对
        Set<Map.Entry<String,Integer>> entrySet=map.entrySet();//泛型套泛型
        for (Map.Entry<String,Integer>  e:entrySet) {
            String key=e.getKey();
            Integer value=e.getValue();
            System.out.println(key+","+value);
        }
        Iterator iterator= entrySet.iterator();
        while (iterator.hasNext()){
            System.out.println(iterator.next());
        }

        //遍历所有value,value是可重复的
        Collection<Integer> values= map.values();
        for (Integer value:values){
            System.out.println("value:"+value);
        }
        Iterator i= values.iterator();
        while (i.hasNext()){
            System.out.println(i.next());
        }

        /*
        集合和map提供了forEach方法,支持lambda表达式
         */
        Collection<Integer> c=new ArrayList<>();
        c.add(0);
        c.add(1);
        c.add(2);
        c.add(3);
        for (Integer i1:c) {
            System.out.println(i1);
        }
        //集合的forEach的方法
        c.forEach(integer -> System.out.println(integer) );
        //map的forEach的方法
        map.forEach((string, integer) -> System.out.println(string+":"+integer) );
    }
}
